﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Zwms.Client.CommonUI;
using Zwms.Data.BusinessObjects;
using Zwms.Data.CommonObject;
using Zwms.Client.InfoWms;

namespace Zwms.Client.OutWms
{
    public partial class OutPlanBillDetailForm : BaseForm
    {
        #region 成员变量
        public BilOutPlanBillDetail CurrentOutPlanBillDetail { get; set; }
        private EnumOpenType OpenType { get; set; }
        private int _spec = 1;
        private string _singleGoodsCode = "";
        #endregion

        #region 构造函数
        public OutPlanBillDetailForm(EnumOpenType opentype, string sOutPlanBillCode, string sGoodsCode)
        {
            InitializeComponent();

            CurrentOutPlanBillDetail = new BilOutPlanBillDetail();

            this.OpenType = opentype;
            if (opentype == EnumOpenType.New)
            {
                CurrentOutPlanBillDetail.OBPlanCode = sOutPlanBillCode;
            }
            else if (opentype == EnumOpenType.Open)
            {
                tbsGoodsCode.SetEditState(false);
                //通过服务读取'入库计划明细'的实例
                ServiceReferenceZwms.ServiceZwmsClient client
                       = new ServiceReferenceZwms.ServiceZwmsClient();
                BilOutPlanBillDetail record = new BilOutPlanBillDetail();
                record.OBPlanCode = sOutPlanBillCode;
                record.GoodsCode = sGoodsCode;

                CurrentOutPlanBillDetail = client.QueryOutPlanBillDetail(record);
                _spec = CurrentOutPlanBillDetail.GoodsSpec;
                _singleGoodsCode = CurrentOutPlanBillDetail.SingleGoodsCode;
            }
            InitBtnImage();
            InitControl();
        }
        #endregion

        #region 私有函数
        private void InitBtnImage()
        {
            this.btnConfirm.Image = global::Zwms.Client.Properties.Resources.ok;
            this.btnClear.Image = global::Zwms.Client.Properties.Resources.cancel2;
            this.btnCancel.Image = global::Zwms.Client.Properties.Resources.cancel;

        }
        private void InitControl()
        {
            if (this.OpenType == EnumOpenType.New)
            {

            }
            else if (this.OpenType == EnumOpenType.Open)
            {
                LoadDetail(CurrentOutPlanBillDetail);
            }

        }

        private void LoadDetail(BilOutPlanBillDetail record)
        {
            this.tbsGoodsCode.SetTextBoxContent(record.GoodsName);
            this.tbsGoodsCode.Tag = record.GoodsCode;
            this.nudGoodsPackageQty.Value = record.GoodsPackageQty;
            this.nudGoodsPieceQty.Value = record.GoodsPieceQty;
            this.nudGoodsQty.Value = record.GoodsQty;
            this.tbRemark.Text = record.Remark;
        }

        public BilOutPlanBillDetail GetDetail()
        {
            if (string.IsNullOrWhiteSpace(tbsGoodsCode.GetTextBoxContent()))
            {
                return null;
            }

            {
                CurrentOutPlanBillDetail.GoodsCode = tbsGoodsCode.Tag.ToString();
                CurrentOutPlanBillDetail.GoodsName = tbsGoodsCode.GetTextBoxContent();
                CurrentOutPlanBillDetail.GoodsSpec = _spec;
                CurrentOutPlanBillDetail.SingleGoodsCode = _singleGoodsCode;
                CurrentOutPlanBillDetail.GoodsPackageQty = Convert.ToInt32(nudGoodsPackageQty.Value);
                CurrentOutPlanBillDetail.GoodsPieceQty = Convert.ToInt32(nudGoodsPieceQty.Value);
                CurrentOutPlanBillDetail.GoodsQty = Convert.ToInt32(nudGoodsQty.Value);
                CurrentOutPlanBillDetail.OutGoodsPackageQty = 0;
                CurrentOutPlanBillDetail.OutGoodsPieceQty = 0;
                CurrentOutPlanBillDetail.OutGoodsQty = 0;
                return CurrentOutPlanBillDetail;
            }
        }

        private void CalculateGoodsQty()
        {
            nudGoodsQty.Value = nudGoodsPackageQty.Value * _spec + nudGoodsPieceQty.Value;
        }


        #endregion

        #region 控件函数
        private void btnConfirm_Click(object sender, EventArgs e)
        {
            bool bRet = true;
            if (string.IsNullOrWhiteSpace(tbsGoodsCode.GetTextBoxContent()))
            {
                bRet = false;
                tbsGoodsCode.SetTextBGColor(Color.Yellow);
            }

            if (!bRet)
            {
                return;
            }
            BilOutPlanBillDetail record = GetDetail();
            if (record == null)
            {
                return;
            }
            ServiceReferenceZwms.ServiceZwmsClient client
                   = new ServiceReferenceZwms.ServiceZwmsClient();
            if (this.OpenType == EnumOpenType.New)
            {
                if (client.AddOutPlanBillDetail(record))
                {
                    MessageBox.Show("成功！");
                }
                else
                {
                    MessageBox.Show("失败，请检查是否已添加此商品，如需区别同类商品，请添加批次信息！");
                    return;
                }
            }
            else if (this.OpenType == EnumOpenType.Open)
            {
                if (client.UpdateOutPlanBillDetail(record))
                {
                    MessageBox.Show("成功！");
                }
                else
                {
                    MessageBox.Show("失败，请检查是否已添加此商品，如需区别同类商品，请添加批次信息！");
                    return;
                }
            }

            this.DialogResult = System.Windows.Forms.DialogResult.OK;
            this.Close();
        }

        private void btnClear_Click(object sender, EventArgs e)
        {
            this.tbsGoodsCode.SetTextBoxContent(string.Empty);
            this.tbsGoodsCode.Tag = string.Empty;
            this.nudGoodsPackageQty.Value = 1;
            this.nudGoodsPieceQty.Value = 1;
            this.nudGoodsQty.Value = 1;
            this.tbRemark.Text = string.Empty;
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.DialogResult = System.Windows.Forms.DialogResult.Cancel;
            this.Close();
        }

        //选择货物
        private void tbsGoodsCode_SelectButtonClick(object sender, EventArgs e)
        {
            GoodsWnd uc = new GoodsWnd();
            CommonUI.ContainerForm window = new CommonUI.ContainerForm(uc, "选择货物");
            uc.SetQueryState();
            if (window.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                tbsGoodsCode.SetTextBoxContent(uc.SelectedRecord.GoodsName);
                tbsGoodsCode.Tag = uc.SelectedRecord.Id;
                _spec = uc.SelectedRecord.Specs;
                _singleGoodsCode = uc.SelectedRecord.SingleGoodsCode;
                CalculateGoodsQty();
            }
        }

        private void nudQty_ValueChanged(object sender, EventArgs e)
        {
            CalculateGoodsQty();
        }
        #endregion

    }
}
